package com.macro.mall.portal.controller;


import com.macro.mall.common.api.CommonResult;
import com.macro.mall.portal.domain.PayDeposit;
import com.macro.mall.portal.domain.RefundDeposit;
import com.macro.mall.portal.domain.vo.PayVo;
import com.macro.mall.portal.service.PayDepositService;
import com.macro.mall.portal.service.RefundDepositService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.dom4j.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 支付定金表 前端控制器
 * </p>
 *
 * @author db
 * @since 2021-09-07
 */
@RestController
@RequestMapping("/payDeposit")
@Api(tags = "微信支付")
public class PayDepositController {

    @Autowired
    private PayDepositService depositService;


    @ApiOperation("支付下单")
    @RequestMapping(value = "/pay", method = RequestMethod.POST)
    public CommonResult pay(@RequestBody PayDeposit payDeposit, HttpServletRequest request) {
        try {
            CommonResult commonResult = depositService.pay(payDeposit, request);
            return commonResult;
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }

    @ApiOperation("查询订单状态")
    @RequestMapping(value = "/queryOrderStatus", method = RequestMethod.POST)
    public CommonResult queryOrderStatus(@RequestBody PayVo payVo) {
        CommonResult commonResult = depositService.queryOrderStatus(payVo);
        return commonResult;
    }


    @RequestMapping(value = "/callBack", method = RequestMethod.POST)
    public String callBack(@RequestBody String reqStr) {
        Document res = depositService.callBack(reqStr);
        return res.getRootElement().asXML();
    }

    @ApiOperation("退款-验证用户支付记录")
    @RequestMapping(value = "/check", method = RequestMethod.POST)
    public CommonResult check(@RequestBody PayVo payVo) {
        CommonResult commonResult = depositService.check(payVo);
        return commonResult;
    }

    @ApiOperation("退款-接口提交")
    @RequestMapping(value = "/refundAdd", method = RequestMethod.POST)
    public CommonResult refundAdd(@RequestBody RefundDeposit refundDeposit) {
        if (StringUtils.isEmpty(refundDeposit.getOutTradeNo())) {
            return CommonResult.failed("订单为空");
        }
        try {
            depositService.add(refundDeposit);
            return CommonResult.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("服务器异常");
        }
    }


    @RequestMapping(value = "/refundCallBack", method = RequestMethod.POST)
    public CommonResult refundCallBack(@RequestBody String params) {

        return CommonResult.success(null);
    }


    @RequestMapping(value = "/partner", method = RequestMethod.POST)
    public CommonResult partner(@RequestBody PayDeposit deposit) {
        depositService.partner(deposit);
        return CommonResult.success(null);
    }
}
